package ynu;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

public class User {
    private String name;
    private String id;
    private String password;
    private String type;
    private String phone;
    private String time;

    public User() {
    }

    public User(String name, String id, String password, String type, String phone, String time) {
        this.name = name;
        this.id = id;
        this.password = password;
        this.type = type;
        this.phone = phone;
        this.time = time;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public boolean login(String id,String password) throws IOException {
        String filePath = "D:\\java实训大作业\\id密码库.txt";
        BufferedReader br = new BufferedReader(new FileReader(filePath));
        String line;
        while((line=br.readLine())!=null){
            String[] mes = line.split(",");
            if(mes[0].equals(id)&&mes[1].equals(password)){
                return true;
            }
        }
        return false;
    }

    public boolean logout(){
        return false;
    }

    //分割用户名
    public String getName(String id,String path) throws IOException {
        String name = "";
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while ((line = br.readLine()) != null) {
            String[] mes = line.split("[,:]");
            if(mes.length > 3){
                if (mes[3].equals(id)) {
                    name = mes[1];
                }
            }
        }
        br.close();
        return name;
    }

    //判断登录者身份
    public boolean judgeIdentity(String id,String path) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while ((line = br.readLine()) != null) {
            String[] mes = line.split("[,:]");
            if(mes.length > 3){
                if (mes[3].equals(id)) {
                    return false;
                }
            }
        }
        return true;
    }

    //判断密码是否为默认密码
    public boolean judgePassword(String id,String password1) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\java实训大作业\\id密码库.txt"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] mes = line.split("[,]");
            if(mes.length > 0){
                if (mes[0].equals(id) && mes[1].equals("123456")) {
                    System.out.println("当前密码为默认密码，请修改你的密码!");
                    return true;
                }
            }
        }
        return false;
    }

    //修改自身密码
    public void changePassword(String password,String id) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\java实训大作业\\id密码库.txt"));
        String line;
        ArrayList<String> list = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            list.add(line);
        }
        br.close();
        BufferedReader br1 = new BufferedReader(new FileReader("D:\\java实训大作业\\id密码库.txt"));
        String line1 = "";
        String line2 = "";
        for(int i = 0; i < list.size(); i++){
            line1 = br1.readLine();
            String[] mes = line1.split(",");
            if(mes.length>0){
                if(mes[0].equals(id)){
                    line2 = mes[0] + "," + password;
                    list.set(i,line2);
                }
            }
        }
        br1.close();
        BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\java实训大作业\\id密码库.txt"));
        writer.write("");
        writer.close();
        BufferedWriter writer1 = new BufferedWriter(new FileWriter("D:\\java实训大作业\\id密码库.txt",true));
        for(int i = 0; i < list.size(); i++){
            writer1.write(list.get(i));
            writer1.newLine();
        }
        writer1.close();
        System.out.println("修改成功！");
    }

    //重置密码
    public void resetPassword(String id) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\java实训大作业\\id密码库.txt"));
        String line;
        ArrayList<String> list = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            list.add(line);
        }
        br.close();
        BufferedReader br1 = new BufferedReader(new FileReader("D:\\java实训大作业\\id密码库.txt"));
        String line1 = "";
        String line2 = "";
        for(int i = 0; i < list.size(); i++){
            line1 = br1.readLine();
            String[] mes = line1.split(",");
            if(mes.length>0){
                if(mes[0].equals(id)){
                    line2 = mes[0] + ",123456";
                    list.set(i,line2);
                }
            }
        }
        br1.close();
        BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\java实训大作业\\id密码库.txt"));
        writer.write("");
        writer.close();
        BufferedWriter writer1 = new BufferedWriter(new FileWriter("D:\\java实训大作业\\id密码库.txt",true));
        for(int i = 0; i < list.size(); i++){
            writer1.write(list.get(i));
            writer1.newLine();
        }
        writer1.close();
        System.out.println("重置成功！");
    }

    //重置密码添加到日志
    public void resetPasswordLog(String resettime,String name,String resetname,String type) throws IOException {
        BufferedWriter writer1 = new BufferedWriter(new FileWriter("D:\\java实训大作业\\日志文件.txt", true));
        String message = "重置时间：" + resettime + ", 重置" +type + name + "的密码, 操作人：" + resetname;
        writer1.write(message);
        writer1.newLine();
        writer1.close();
    }

    public static void logOperation(String operationType, String operationDetail, String operator) {
        try {
            String logPath = "D:\\java实训大作业\\日志文件.txt";
            File logFile = new File(logPath);
            boolean isNewFile = !logFile.exists();

            try (PrintWriter out = new PrintWriter(new FileWriter(logFile, true))) {
                LocalDateTime now = LocalDateTime.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

                if (isNewFile) {
                    out.println("时间,操作类型,操作内容,操作人");
                }

                out.println(now.format(formatter) + "," +
                        operationType + "," +
                        operationDetail + "," +
                        operator);
            }
        } catch (IOException e) {
            System.err.println("日志写入失败: " + e.getMessage());
        }
    }
}
